using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class CityBuildingUpgradeUI : MonoBehaviour
{
	public GameObject CurrentState;

	public GameObject Arrow;

	public GameObject NextState;

	public TextMeshProUGUI CurrentGainText;

	public TextMeshProUGUI NextGainText;

	public TextMeshProUGUI NextCost;

	public Button UpgradeButton;

	public TextMeshProUGUI Title;

	public TextMeshProUGUI LevelText;

	public Color AvailableColor;

	public TextMeshProUGUI RVPrice;

	public GameObject RVDock;

	private CityBuildingElementHandler _currentHandler;

	private void Awake()
	{
		base.gameObject.SetActive(value: false);
		CityBuildingManager instance = CityBuildingManager.Instance;
		instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Combine(instance.OnElementSelected, new Action<CityBuildingElementHandler>(Setup));
		CityBuildingManager instance2 = CityBuildingManager.Instance;
		instance2.OnCollectGems = (Action)Delegate.Combine(instance2.OnCollectGems, new Action(OnCollect));
	}

	private void OnDestroy()
	{
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager instance = CityBuildingManager.Instance;
			instance.OnElementSelected = (Action<CityBuildingElementHandler>)Delegate.Remove(instance.OnElementSelected, new Action<CityBuildingElementHandler>(Setup));
			CityBuildingManager instance2 = CityBuildingManager.Instance;
			instance2.OnCollectGems = (Action)Delegate.Remove(instance2.OnCollectGems, new Action(OnCollect));
		}
	}

	public void OnCollect()
	{
		Setup(_currentHandler);
	}

	public void Setup(CityBuildingElementHandler elem)
	{
		if (_currentHandler != elem)
		{
			base.gameObject.SetActive(value: false);
		}
		_currentHandler = elem;
		if (elem == null)
		{
			return;
		}
		CityBuildingElementData data = elem.Data;
		if (data.Unlocked)
		{
			Title.text = "Upgrade";
			LevelText.text = "Level " + (data.Level + 1);
			if (data.Level >= elem.Visual.Levels.Count - 1)
			{
				LevelText.text = "MAXED";
				Arrow.SetActive(value: false);
				NextState.SetActive(value: false);
				CurrentState.SetActive(value: true);
				CurrentGainText.text = data.GainPerHoursString;
			}
			else
			{
				Arrow.SetActive(value: true);
				NextState.SetActive(value: true);
				CurrentState.SetActive(value: true);
				CurrentGainText.text = data.GainPerHoursString;
				NextCost.text = data.UpgradePrice().ToString();
				NextCost.color = ((CityBuildingManager.Instance.Gems >= data.UpgradePrice()) ? AvailableColor : Color.red);
				NextGainText.text = data.NextLevelGainPerHoursString;
				UpgradeButton.interactable = CityBuildingManager.Instance.Gems >= data.UpgradePrice() / 3;
				RVPrice.text = (data.UpgradePrice() / 3).ToString();
				RVDock.SetActive(CityBuildingManager.Instance.Gems >= data.UpgradePrice() / 3 && CityBuildingManager.Instance.Gems < data.UpgradePrice());
			}
		}
		else
		{
			CurrentState.SetActive(value: false);
			Arrow.SetActive(value: false);
			NextState.SetActive(value: true);
			NextCost.text = data.UnlockPrice.ToString();
			NextCost.color = ((CityBuildingManager.Instance.Gems >= data.UnlockPrice) ? AvailableColor : Color.red);
			NextGainText.text = data.GainPerHoursString;
			UpgradeButton.interactable = CityBuildingManager.Instance.Gems >= data.UnlockPrice / 3;
			RVPrice.text = (data.UnlockPrice / 3).ToString();
			RVDock.SetActive(CityBuildingManager.Instance.Gems >= data.UnlockPrice / 3 && CityBuildingManager.Instance.Gems < data.UnlockPrice);
			Title.text = "Unlock";
		}
		base.gameObject.SetActive(value: true);
	}

	public void OnUpgradeClicked()
	{
		CityBuildingElementData data = _currentHandler.Data;
		if (data.Unlocked)
		{
			if (data.Level >= _currentHandler.Visual.Levels.Count - 1)
			{
				return;
			}
			if (CityBuildingManager.Instance.Gems >= data.UpgradePrice())
			{
				CityBuildingManager.Instance.Gems -= data.UpgradePrice();
				_currentHandler.LevelUp();
			}
			else if (CityBuildingManager.Instance.Gems >= data.UpgradePrice() / 3)
			{
				if (SingletonMB<GameManager>.Instance.BetterNoAds)
				{
					OnUpgradeClickedRVCallback(success: true);
				}
				else
				{
					OnUpgradeClickedRVCallback(success: true);
				}
				return;
			}
		}
		else if (CityBuildingManager.Instance.Gems >= data.UnlockPrice)
		{
			CityBuildingManager.Instance.Gems -= data.UnlockPrice;
			_currentHandler.Unlock();
		}
		else if (CityBuildingManager.Instance.Gems >= data.UnlockPrice / 3)
		{
			if (SingletonMB<GameManager>.Instance.BetterNoAds)
			{
				OnUpgradeClickedRVCallback(success: true);
			}
			else
			{
				OnUpgradeClickedRVCallback(success: true);
			}
			return;
		}
		CityBuildingManager.Instance.Select(_currentHandler);
		_currentHandler.Visual.DoUpgradeAnim();
	}

	private void OnUpgradeClickedRVCallback(bool success)
	{
		CityBuildingElementData data = _currentHandler.Data;
		if (data.Unlocked)
		{
			CityBuildingManager.Instance.Gems -= data.UpgradePrice() / 3;
			_currentHandler.LevelUp();
		}
		else
		{
			CityBuildingManager.Instance.Gems -= data.UnlockPrice / 3;
			_currentHandler.Unlock();
		}
		CityBuildingManager.Instance.Select(_currentHandler);
		_currentHandler.Visual.DoUpgradeAnim();
	}

	public void OnCloseClicked()
	{
		base.gameObject.SetActive(value: false);
		_currentHandler = null;
		CityBuildingManager.Instance.Select(null);
	}
}
